# Maintainer: Johannes Löthberg <johannes@kyriasis.com>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - explicitly link v5/v6 with libatomic

pkgname=haproxy
pkgver=2.3.10
pkgrel=1

pkgdesc='Reliable, high performance TCP/HTTP load balancer'
url='https://www.haproxy.org/'
arch=('x86_64')
license=('GPL')

depends=('openssl' 'pcre' 'zlib' 'libxcrypt' 'libcrypt.so' 'lua' 'systemd-libs')
makedepends=('systemd')

backup=('etc/haproxy/haproxy.cfg')

install=haproxy.install

source=(https://www.haproxy.org/download/${pkgver%.*}/src/haproxy-$pkgver.tar.gz
        haproxy.cfg
        haproxy.sysusers
        0001-Use-CFLAGS-and-LDFLAGS-when-building-contrib.patch)

sha256sums=('9946e0cfc83f29072b3431e37246221cf9d4a9d28a158c075714d345266f4f35'
            'e88edeb2fe23508565e46ff485c93cfb7c2e9c64f231538452ec7ecd7ec37a42'
            'b02b4088205039f0979aef0ef3e22957f493dd54eabefaee7011f2d795303b3e'
            '81c03ef5d69f4f2ef2c7caa6722c82cda4a53b65f788a46e668ec2faa3abd8f5')

prepare() {
  cd haproxy-$pkgver

  patch -p1 <"$srcdir"/0001-Use-CFLAGS-and-LDFLAGS-when-building-contrib.patch
}

build() {
  cd haproxy-$pkgver

  [[ $CARCH == "arm" || $CARCH == "armv6h" ]] && ADDLIB="-latomic"
  make CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" \
    ADDLIB=$ADDLIB \
    CPU=generic \
    TARGET=linux-glibc \
    USE_GETADDRINFO=1 \
    USE_OPENSSL=1 \
    USE_PCRE=1 USE_PCRE_JIT=1 \
    USE_ZLIB=1 \
    USE_LUA=1 \
    USE_SYSTEMD=1 \
    EXTRA_OBJS="contrib/prometheus-exporter/service-prometheus.o"

  for contrib in halog iprange ip6range systemd; do
    make -C contrib/$contrib SBINDIR=/usr/bin OPTIMIZE= CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"
  done
}

package() {
  cd haproxy-$pkgver
  make \
    PREFIX="$pkgdir"/usr \
    SBINDIR="$pkgdir"/usr/bin \
    DOCDIR="$pkgdir"/usr/share/$pkgname \
    install

  for contrib in halog iprange ip6range; do
    install -Dm755 contrib/$contrib/$contrib "$pkgdir"/usr/bin/$contrib
  done

  install -Dm644 ../haproxy.sysusers "$pkgdir"/usr/lib/sysusers.d/haproxy.conf
  install -Dm644 ../haproxy.cfg "$pkgdir"/etc/haproxy/haproxy.cfg
  install -Dm644 contrib/systemd/haproxy.service \
    "$pkgdir"/usr/lib/systemd/system/haproxy.service
  cp -r examples "$pkgdir"/usr/share/haproxy/examples
}
